/**h* DesfireAPI/Legacy
 *
 * NAME
 *   DesfireAPI :: Specific entry points for Legacy re-entrant and non-reentrant DLL
 *
 * COPYRIGHT
 *   (c) 2009 SpringCard - www.springcard.com
 *
 * SEE ALSO
 *   LegacyEX
 *
 **/

#include "sprox_desfire_i.h"

#ifndef SPROX_DESFIRE_PCSC

#ifndef SPROX_API_REENTRANT 
  SPROX_DESFIRE_CTX_ST desfire_ctx = { 0xFF, 0xFF, 0, { 0 }, KEY_EMPTY, KEY_UNSET, 0, 0, { 0 }, { 0 }, { 0 }, { 0 } };
#endif

/**f* DesfireAPI/[Legacy]SelectCid
 *
 * NAME
 *   [Legacy]SelectCid
 *
 * DESCRIPTION
 *   Selects the logical number of the addressed DesFire card
 *   CID is used to distinguish several Desfire cards simultaneously selected by a single reader
 *
 * SYNOPSIS
 *
 *   [[sprox_desfire.dll]]
 *   SWORD SPROX_Desfire_SelectCid (BYTE cid);
 *
 *   [[sprox_desfire_ex.dll]]
 *   SWORD SPROXx_Desfire_SelectCid(SPROX_INSTANCE rInst, BYTE cid);
 *
 *   [[pcsc_desfire.dll]]
 *   Not applicable.
 * 
 * INPUTS
 *   BYTE cid        : logical number of the addressed DesFire card
 *
 * RETURNS
 *   DF_OPERATION_OK : CID selected
 *
 **/
SPROX_API_FUNC(Desfire_SelectCid) (SPROX_PARAM  BYTE cid)
{
  SPROX_DESFIRE_GET_CTX();
  ctx->tcl_cid = cid;
  return DF_OPERATION_OK;
}


/**f* DesfireAPI/[Legacy]IsoWrapping
 *
 * NAME
 *   [Legacy]IsoWrapping
 *
 * DESCRIPTION
 *   Select the wrapping mode of Desfire legacy commands into ISO 7816-4 APDUs.
 *
 * SYNOPSIS
 *
 *   [[sprox_desfire.dll]]
 *   SWORD SPROX_Desfire_IsoWrapping (BYTE mode);
 *
 *   [[sprox_desfire_ex.dll]]
 *   SWORD SPROXx_Desfire_IsoWrapping(SPROX_INSTANCE rInst, BYTE mode);
 *
 *   [[pcsc_desfire.dll]]
 *   Not applicable. See Desfire_IsoWrapping[PC/SC].
 *
 * INPUTS
 *   BYTE mode             : ISO wrapping mode (DF_ISO_WRAPPING_CARD or DF_ISO_WRAPPING_READER)
 *
 * RETURNS
 *   DF_OPERATION_OK       : ISO wrapping mode set
 *   DFCARD_LIB_CALL_ERROR : ISO wrapping mode not set, mode not available
 *
 * NOTES
 *   DF_ISO_WRAPPING_CARD uses the card's wrapping method (see chapter 3.13 
 *   of datasheet of mifare DesFire MF3ICD40 for more information).
 *
 *   DF_ISO_WRAPPING_OFF disables the wrapping (native legacy command are used).
 *   This is the default mode.
 *
 **/

SPROX_API_FUNC(Desfire_IsoWrapping) (SPROX_PARAM  BYTE mode)
{
  SPROX_DESFIRE_GET_CTX();
  
  switch (mode)
  {
    case DF_ISO_WRAPPING_OFF    : break;
    case DF_ISO_WRAPPING_CARD   : break;

    case DF_ISO_WRAPPING_READER :
    default                     : return DFCARD_LIB_CALL_ERROR;    
  }

  ctx->iso_wrapping = mode;
  return DF_OPERATION_OK;
}

SPROX_API_FUNC(Desfire_Exchange) (SPROX_PARAM_V)
{
  SPROX_RC status;
  WORD send_len, recv_len;
  SPROX_DESFIRE_GET_CTX();
  
  if (ctx->xfer_length > 65535)
    return DFCARD_WRONG_LENGTH;
    
  send_len = (WORD) ctx->xfer_length;  
  recv_len = sizeof(ctx->xfer_buffer);

  if (ctx->xfer_fast)
  {
    status = SPROX_API_CALL(TclA_ExchangeDF) (SPROX_PARAM_P  ctx->tcl_cid, ctx->xfer_buffer, send_len, ctx->xfer_buffer, &recv_len);
  } else
  {
    status = SPROX_API_CALL(TclA_Exchange) (SPROX_PARAM_P  5, ctx->tcl_cid, 0xFF, ctx->xfer_buffer, send_len, ctx->xfer_buffer, &recv_len);
  }
  
  ctx->xfer_length = recv_len;
  return status;
}

#endif
